/**
*******************************************************************
Copyright (C) 2011  Elizar D. Pepino

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
 
*********************************************************************
* @description:	Basic slide plugin for jquery
* @author: Elizar Pepino
* @version: 0.0.1
* @site: http://d2980014.u81.secureserverdot.com/ ( my temporary site domain name already expired )
* 
* @parameters
*
*	(int)		itemsToDisplay		- number of items to display
*	(int)		itemsToScroll		- number of items per scroll
*	(string)	prevBtn				- classname or id of the element that will be used as a previous button 
*	(string)	nextBtn				- classname or id of the element that will be used as a next button
*	(str/int)	speed				- animation speed
*	(string)	easing				- easing that will used for the animation 
*
* @Log:
*			
**/

jQuery.fn.extend({
	basicslide: function(options) {
		return this.each(function() {
			new jQuery.BasicSlide(this, options);
		});
	}
});


jQuery.BasicSlide = function(target, options) {

	
	// Setup vars
	var _target = $(target);
	var _numOfChildren;
	var _numOfPages;
	var _baseWidth;
	var _itemContainerWidth;
	var _currentPage = 0;
	var _itemRightMargin;
	var isAnimating = false;

	// Setup options
	var _options = options || {};
	_options.itemsToDisplay = _options.itemsToDisplay || 1;
	_options.itemsToScroll = _options.itemsToScroll || _options.itemsToDisplay;
	_options.prevBtn = _options.prevBtn || ".basicslide-prevBtn";
	_options.nextBtn = _options.nextBtn || ".basicslide-nextBtn";
	_options.speed = _options.speed || 1000;
	_options.easing = _options.easing || "easeInOutExpo";
	
	var _prev = $(_options.prevBtn);
	var _next = $(_options.nextBtn);
	
	// Initial logic and setups
	_itemRightMargin = parseInt(_target.children('li:first-child').css('marginRight'));
	_numOfChildren = _target.children('li').length;
	_numOfPages = Math.ceil(_numOfChildren / _options.itemsToDisplay);
	_baseWidth = _target.children('li:first-child').outerWidth(true);
	_itemContainerWidth = _numOfChildren*_baseWidth;
	
	
	// for debugging purposes only
	// console.log("Number of children: "+_numOfChildren+"\nNumber of pages: "+_numOfPages+"\n Base width: "+_baseWidth+"\nContainer Width: "+_itemContainerWidth+"\nRight margin: "+_itemRightMargin+"\n\r--------------------------");
	
	// Setup buttons
	_prev.css("z-index","1").click(function(){
		if (!isAnimating)
		doSlide("left");
		
		return false;
	});
	_next.css("z-index","1").click(function(){
		if (!isAnimating)
		doSlide("right");
		
		return false;
	});
	
	
	// Setup Layout
	_target.css({
		position:(_target.css('position') == "absolute") ? "absolute" : "relative",
		overflow:"hidden",
		width:_options.itemsToScroll*_baseWidth-_itemRightMargin,
		paddingBottom:2
		});
	
	// setup _target's child elements
	_target.children().each(function(index){
		var child = $(this);
		child.css({
			position:"absolute",
			top:child.css("top"),
			zIndex:"0",
			left:index*_baseWidth+"px"
		});
	})
	
	// Methods
	function doSlide(direction)
	{
		
		// check if _target is visible then apply animation
		if (_target.is(":visible") || _target.css("visibility") == "hidden") {
			
			isAnimating = true;
			
			// move each of the target's child either left or right
			var distance = _options.itemsToScroll*_baseWidth;
			var baseX = parseInt(_target.children(":first-child").css("left"));
			
			if (direction == "left") {
				if (baseX < 0) {
					if (Math.abs(baseX) < _options.itemsToScroll*_baseWidth) {
						distance = Math.abs(baseX);
					}
					else
					{
						distance = distance;
					}
				}
				else
				{
					distance = 0;
				}
			}
			else
			{
				var offset = _itemContainerWidth - (Math.abs(baseX) + _target.width());
				distance = _target.width() - offset > 0 ? offset*-1  + _itemRightMargin: distance*-1;
			}
		
			_target.children().each(function(index){
				var child = $(this);
				child.animate({
					left: '+='+distance
				},
				{
					speed:_options.speed,
					easing:_options.easing,
					complete:function(){
						isAnimating = false;
					}
				});
			})
		};
	}
	

}